from flask import current_app,request,jsonify

from Async import app_celery 
from . import copernicus_app
import ToolBox as tb


@copernicus_app.route('/searcher/qhdm',methods=['GET','post'])
def province_2_products():
    cargs = current_app.config['Cargs']
    agent = current_app.config['Agent']
    proxies = {
        'http': f"{agent['ip']}:{agent['port']}",
        'https': f"{agent['ip']}:{agent['port']}"}
    data = request.get_json()
    qhdm = data["qhdm"]
    start = data["sdate"]
    end = data["edate"]
    cloud = data["cloud"]
    tb.qhdm_2_table.delay(cargs,start,end,cloud,proxies)
    msg={'msg':"Success!",'code':200,"data":records} 
    return jsonify(msg)


@copernicus_app.route('/searcher/update',methods=['GET','post'])
def searcher_update():
    cargs = current_app.config['Cargs']
    agent = current_app.config['Agent']
    proxies = {
        'http': f"{agent['ip']}:{agent['port']}",
        'https': f"{agent['ip']}:{agent['port']}"}
    jsondata = request.get_json()
    qhdm = jsondata["qhdm"]
    start = jsondata["sdate"]
    end = jsondata["edate"]
    cloud = jsondata["cloud"]
    task = app_celery.send_task(
        'ToolBox.qhdm_2_table', 
        args=[cargs,qhdm,start,end,cloud,proxies])
    msg={'msg':"Success!",'code':200,"taskid":task.id} 
    return jsonify(msg)


@copernicus_app.route('/searcher/tile',methods=['GET','post'])
def searcher_tile():
    agent = current_app.config['Agent']
    proxies = {
        'http': f"{agent['ip']}:{agent['port']}",
        'https': f"{agent['ip']}:{agent['port']}"}
    data = request.get_json()
    tile = data["tile"]
    start = data["sdate"]
    end = data["edate"]
    cloud = data["cloud"]
    # task = app_celery.send_task(
    #     'ToolBox.tile_2_info', 
    #     args=[tile,start,end,cloud,proxies])
    data = tb.tile_2_info(tile,start,end,cloud,proxies)
    msg={'msg':"Success!",'code':200,"data":data}  
    return jsonify(msg)